<!DOCTYPE html>
<html>
<head>
    {include file="../application/common/header.html" /}
</head>

<body>
    <div id="course_managetb">
        <form id="course_manageForm" class="m10">
            课程名称:<input id="courseName" name="courseName" class="easyui-validatebox" />
            课程状态:<select id="isAgree" name="isAgree" class="form-control1 ">
                <option value="">请选择</option>
                <option value="0">待审批</option>
                <option value="1">同意</option>
                <option value="2">不同意</option>
            </select>
            <a class="easyui-linkbutton l-btn l-btn-small" data-options="iconCls:'icon-search'" onclick="searchForm('course_manage')">查询</a>
            <a class="easyui-linkbutton l-btn l-btn-small" data-options="iconCls:'icon-clear'" onclick="resetForm('course_manage')">重置</a>
            <a class="easyui-linkbutton l-btn l-btn-small" data-options="iconCls:'icon-save'" onclick="outExcel('course_manage','/kccsCourse/course_manage')">导出为EXCEL</a>
            <a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-large-smartart',plain:true" onclick="upload()">导入</a>
        </form>
    </div>
    <table id="course_managedg">
        <thead>
            <tr>
                <th data-options="field:'ID',checkbox:true"></th>
                <th data-options="field:'IS_AGREE',title:'课程状态',
                editor:{
                    type:'combobox',
                    options: {  
                        data: [{'value': '1', 'text': '同意'},{'value': '2', 'text': '不同意'}],  
                        editable: false,  
                        panelHeight:70,  
                        required: true  
                    } 
                },formatter:function(value,row,index){
                    if(value == 0) return '待审核';
                    else if(value == 1) return '同意';
                    else if(value == 2) return '不同意';
                    return '';
                }"></th>
                <th data-options="field:'COURSE_NAME',title:'课程名称',
                editor:{type:'textbox',required:true}"></th>
                <th data-options="field:'COURSE_SUBJECT',title:'课程学科',
                editor:{
                    type:'combobox',
                    options:{
                        method:'get',
                        url: '/compent/util/subjectList',
                        mode:'remote',
                        multiple:true,
                        editable:false,
                        required:true
                    }
                }"></th>
                <th data-options="field:'COURSE_TYPE',title:'课程类型',
                editor:{type:'textbox',required:true}"></th>
                <th data-options="field:'COURSE_NO',title:'课程编号',
                editor:{type:'textbox',required:true}"></th>
                <th data-options="field:'CLASS_TEACHER_IDS',title:'上课教师',
                editor:{
                    type:'combobox',
                    options:{
                        valueField:'id',
                        textField:'name',
                        method:'get',
                        url: '/compent/util/teacherList',
                        mode:'remote',
                        multiple:true,
                        editable:false,
                        required:true
                    }
                },formatter : function(value,row,index){
                    return row.CLASS_TEACHER;
                }"></th>
                <th data-options="field:'CLASS_ADDR',title:'上课地址',
                editor:{type:'textbox',required:true}"></th>
                <th data-options="field:'CLASS_DATE',title:'上课时间',
                editor:{
                    type:'combobox',
                    options: {

                        method:'get',
                        url: '/kccsCourse/course_manage/classDate.html',
                        mode:'remote',
                        multiple:true,
                        editable:true
                    } 
                }"></th>
                <th data-options="field:'LIMIT_NUM',title:'限制人数',
                editor:{type:'numberbox',required:true}"></th>
                <th data-options="field:'COURSE_DESC',title:'课程描述',multiline:true,
                editor:{type:'textbox'},
                formatter: function(value, row, index) {
                    return '<span class=\'note\' title=\''+value+'\'>'+ value + '</span>';
                }"></th>
                <th data-options="field:'CREATE_TIME',title:'创建时间'"></th>
                <th data-options="field:'APPROVER',title:'审核人'"></th>
                <th data-options="field:'UPDATE_TIME',title:'修改时间'"></th>
            </tr>
        </thead>
    </table>
</div>
<div id="uploadBox" style="display: none;height: auto;">
    <div class="m30">
        <form method="post" enctype="multipart/form-data" id="course_manageUploadForm" action="">
            <div class="mt20">
                <input class="easyui-filebox" data-options="buttonText:'选择文件'" id="files" name="files" style="width:250px;">
            </div>
        </form>
    </div>
    <div class="tnc m20">
        <a href="javascript:void(0)" class="easyui-linkbutton" onclick="uploadCommit('course_manage')" style="width: 200px;">上传</a>
    </div>
</div>
<script type="text/javascript">
    var url = "/kccsCourse/course_manage";
    var cf = "course_manage";
    $(function(){
        loadDatagrid("course_manage", url);
    })
    function loadDatagrid(cf, url){
        var btnArr = [];
        btnArr.push({text:'修改',iconCls:'icon-edit',handler:function(){modify(cf)}},'-');
        btnArr.push({text:'保存',iconCls:'icon-save',handler:function(){save(cf)}},'-');
        btnArr.push({text:'删除',iconCls:'icon-remove',handler:function(){del(cf,url)}},'-');
        btnArr.push({text:'取消',iconCls:'icon-undo',handler:function(){undo(cf)}},'-');  
        btnArr.push({text:'同意',iconCls:'icon-ok',handler:function(){ok(cf,url)}});
        btnArr.push('-',{text:'不同意',iconCls:'icon-no',handler:function(){no(cf,url)}});
        $("#"+cf+"dg").datagrid({
            url: url + "/pageQuery.html",
            rownumbers: true,
            striped: true,
            border: false,
            pagination: true,
            pageSize: 30,
            fitColumns: true,
            autoRowHeight: false,
            singleSelect: false,
            fit: true,
            toolbar: btnArr,
            onAfterEdit: function(rowIndex,rowData,changes){
                if($.isEmptyObject(changes)) {
                    var dgObj=$('#'+cf+'dg');
                    dgObj.datagrid('rejectChanges');
                    dgEditIndex[cf]=-1;
                    return; 
                }
                changes.ID = rowData.ID;
                addmodify(changes, cf, url)
            },
            onLoadSuccess: function(data){
                $(".note").tooltip({
                    onShow: function () {
                        $(this).tooltip('tip').css({
                            borderColor: '#000'
                        });
                    }
                });
            }
        })
        var tb = $("#"+cf+"tb").html();
        if(tb) {
            $(".datagrid-toolbar").prepend(tb);
            $("#"+cf+"tb").remove();
        }
    }
    var uploadBox;
    function upload() {
        uploadBox = $('#uploadBox').dialog({
            title : '上传课程分类',
            modal : true,
            width : 400,
            top : 200,
            draggable : true,
            resizable : true,
            buttons : '#btns',
            onClose : function() {
                $('#files').filebox('setValue','');
            }
        }).show();
    }
    
    function uploadCommit(){
        var fileObj = document.getElementById('filebox_file_id_2').files[0];
        if(!fileObj) {
            $.messager.alert('提醒','请选择文件');
            return;
        }
        $.messager.progress({
            title : '提示',
            msg : '上传中，请稍候...'
        });
        var xhr = new XMLHttpRequest();
        xhr.open('POST', url + "/import");
        xhr.onload = function () {
           if (xhr.status === 200) {
                $.messager.progress('close');
                var resp = JSON.parse(xhr.response);
                if(resp.code == '000') {
                    $.messager.alert("成功","上传成功！","info");
                    reload("course_manage");
                    $('#files').filebox('setValue','');
                    uploadBox.dialog('close');
                }else {
                    $.messager.alert('错误提醒', resp.msg);
                }
           }
       };
       xhr.onerror = function () {
           $.messager.progress('close');
           $.messager.alert('错误', "上传异常");
       };
       var fd = new FormData();
       fd.append('files', fileObj);
       // 触发上传
        setTimeout(function() {
            xhr.send(fd);
        }, 1000)
    }
    function ok(cf,url){
        if(dgEditIndex[cf]>-1){
            $.messager.alert('提示','请先保存或取消编辑！','info');
        }else{
            var dgObj=$('#'+cf+'dg');
            var rows=dgObj.datagrid('getSelections');
            if(rows.length>0){
                $.messager.confirm("请确认","你确定同意已选择的课程吗？",function(b){
                    if(b){
                        var delIds=[];
                        for(var i=0;i<rows.length;i++){
                            delIds.push(rows[i].ID);
                        }
                        $.ajax({
                           type:"POST",
                           url: url + "/openClass",
                           data:{courseIds:delIds.join(",")},
                           success: function(r){
                                if(r == '000') {
                                    $.messager.alert('提示','提交成功！','info');
                                    dgObj.datagrid('reload');
                                    return;
                                }
                                $.messager.alert('提示', "失败",'error');
                                undo(cf);
                            }
                        });
                    }
                })
            }else{
                $.messager.alert('提示','请选择行！','error');
            }
        }
    }
    
    function no(cf,url){
        if(dgEditIndex[cf]>-1){
            $.messager.alert('提示','请先保存或取消编辑！','info');
        }else{
            var dgObj=$('#'+cf+'dg');
            var rows=dgObj.datagrid('getSelections');
            if(rows.length>0){
                $.messager.confirm("请确认","你确定不同意已选择的课程吗？",function(b){
                    if(b){
                        var delIds=[];
                        for(var i=0;i<rows.length;i++){
                            delIds.push(rows[i].ID);
                        }
                        $.ajax({
                           type:"POST",
                           url: url + "/stopClass",
                           data:{courseIds:delIds.join(",")},
                           success: function(r){
                                if(r == '000') {
                                    $.messager.alert('提示','提交成功！','info');
                                    dgObj.datagrid('reload');
                                    return;
                                }
                                $.messager.alert('提示', "失败",'error');
                                undo(cf);
                            }
                        });
                    }
                })
            }else{
                $.messager.alert('提示','请选择行！','error');
            }
        }
    }
    function importExcel() {
        $('#uploadBox').dialog({
            title : '上传成绩',
            modal : true,
            width : 400,
            top : 100,
            buttons : '#btns'
        }).show();
    }
    function uploadCommit(){
        $.messager.progress({
            title : '提示',
            msg : '上传中，请稍候...'
        });
        $('#'+cf+'UploadForm').form('submit',{
            url: url + "/importExcel.html",success:function(data){
            $.messager.progress('close');
            if(data == '000') {
                $('#uploadBox').dialog("close");
                searchForm('course_manage');
                $.messager.show({title:'操作提示', msg:"上传成功"});
                return;
            }
            $.messager.alert('提示', data);
        }})
    }
</script>

</body>
</html>